/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.resourceInfo;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.MgrInterfaceGeneratorContext;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;

public class ModelInfoCtorGenerator extends ClassMethodGenerator {

  private String className;
  private String currentSu;
  private String resMetaDataId;
  private String metaDataId;
  private String rootNodeCode;
  private String displayKey;
  private CefApiGeneratorContext parentContext;

  protected ModelInfoCtorGenerator(String className, String currentSu, String resMetadataId,
                                   String rootNodeCode, String displayKey, CefApiGeneratorContext parentContext) {
    this(className, currentSu, resMetadataId, "", rootNodeCode, displayKey, parentContext);
  }

  protected ModelInfoCtorGenerator(String className, String currentSu, String resMetadataId, String metaDataId,
      String rootNodeCode, String displayKey, CefApiGeneratorContext parentContext) {
    this.className = className;
    this.currentSu = currentSu;
    this.resMetaDataId = resMetadataId;
    this.metaDataId = metaDataId;
    this.rootNodeCode = rootNodeCode;
    this.displayKey = displayKey;
    this.parentContext = parentContext;
  }

  @Override
  protected TypeInfo getReturnType() {
    return null;
  }

  @Override
  protected String getMethodName() {
    return className;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return new ArrayList();
  }

  @Override
  protected boolean buildIsConstructor() {
    return true;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
    superConstructorInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, currentSu));
    superConstructorInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, resMetaDataId));
    superConstructorInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, metaDataId));
//        superConstructorInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, rootNodeCode));
    superConstructorInvocation.arguments().add(ExpressionUtils.getStringLiteral(ast, displayKey));

    block.statements().add(superConstructorInvocation);

    MediateType entityType = parentContext.getCefGenContext()
        .getEntityResourceInfos().get(rootNodeCode);
    String typeName = entityType.getFullName();
    TypeInfo type = new TypeInfo(typeName);
    type.setUseFullName(true);

    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setName(ast.newSimpleName("setRootEntityResInfo"));
    ClassInstanceCreation creation = ast.newClassInstanceCreation();
    creation.setType(type.getType(ast, parentContext));
    creation.arguments().add(ast.newThisExpression());
    invocation.arguments().add(creation);
    block.statements().add(ast.newExpressionStatement(invocation));
    return block;
  }
}
